package com.unittec.zk.provider.auth.service;

import com.unittec.zk.provider.auth.common.LogServiceIdConstants;
import com.unittec.zk.provider.common.Md5;
import com.unittec.zk.provider.log.Log;
import com.unittec.zk.provider.scope.ThreadLocalScope;
import com.unittec.zk.provider.service.HystrixRestTemplateService;
import com.unittec.zk.sdk.root.common.FieldConstants;
import com.unittec.zk.sdk.root.domain.GatewayRequest;
import com.unittec.zk.sdk.root.domain.Result;
import com.unittec.zk.sdk.root.enums.LogLevelE;
import com.unittec.zk.sdk.root.internal.exception.BusinessException;
import com.unittec.zk.sdk.log.request.Req102011;
import com.unittec.zk.sdk.log.request.Req102012;
import com.unittec.zk.sdk.log.request.Req102020;
import com.unittec.zk.sdk.log.response.Res102011;
import com.unittec.zk.sdk.log.response.Res102012;
import com.unittec.zk.sdk.log.response.Res102020;
import com.unittec.zk.sdk.log.response.Res102021;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author: chenjiawang
 */
@Service
public class LogService {
    @Autowired
    protected HystrixRestTemplateService hystrixRestTemplateService;

    @Autowired
    protected Log log;

    @Autowired
    private SysServiceRouterServer sysServiceRouterServer;

    /**
     * 应用创建日志
     *
     * @param appId   应用ID
     * @param logName 日志名称
     * @return
     */
    public Boolean appCreateLog(final String appId, final String logName) {
        GatewayRequest<Req102011> getWay = new GatewayRequest<>();
        Req102011 req102011 = new Req102011();
        req102011.setAppId(appId);
        req102011.setLogName(logName);
        getWay.setContent(req102011);
        getWay.setMethod("102011");
        Result<Res102011> result = hystrixRestTemplateService.exchange(Md5.getMd5(FieldConstants.SYS_REQUEST_SOURCE_ID), LogServiceIdConstants.LOG_102011, getWay,
                ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res102011>>() {
                });
        if (!result.isSuccess()) {
            log.error(getWay, result.getBizCode(), result.getBizMsg());
            throw new BusinessException(StringUtils.isBlank(result.getBizMsg()) ? "应用创建日志失败" : result.getBizMsg());
        }
        return result.getData() == null ? false : result.getData().getABoolean();
    }

    /**
     * 配置应用的指定服务日志级别
     *
     * @param serviceIds 多个服务名称
     * @param logLevelE  OFF(99,"OFF"),DEBUG(1, "DEBUG"),INFO(2, "INFO"),WARING(3, "WARNIG"),ERROR(4, "ERROR");
     * @return
     */
    public Boolean setLogLevel(final String[] serviceIds, final String appId, final LogLevelE logLevelE) {
        GatewayRequest<Req102012> getWay = new GatewayRequest<>();
        Req102012 req102012 = new Req102012();
        req102012.setServiceId(serviceIds);
        req102012.setLogLevel(logLevelE);
        req102012.setAppId(appId);
        getWay.setContent(req102012);
        getWay.setMethod("102012");
        Result<Res102012> result = hystrixRestTemplateService.exchange(Md5.getMd5(FieldConstants.SYS_REQUEST_SOURCE_ID), LogServiceIdConstants.UPDATE_LOG_LEVEL_102012,
                getWay, ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res102012>>() {
                });
        if (!result.isSuccess()) {
            log.error(getWay, result.getBizCode(), result.getBizMsg());
            throw new BusinessException(
                    StringUtils.isBlank(result.getBizMsg()) ? "配置应用的指定服务日志级别失败" : result.getBizMsg());
        }
        return result.getData() == null ? false : result.getData().getABoolean();
    }

    /**
     * 根据应用appID获取各个服务的日志级别
     *
     * @param appId
     * @return
     */
    public List<Res102020> getLogLevelListByAppId(final String appId) {
        GatewayRequest<Req102020> getWay = new GatewayRequest<>();
        Req102020 req102020 = new Req102020();
        req102020.setAppId(appId);
        getWay.setContent(req102020);
        getWay.setMethod("102020");
        Result<List<Res102020>> result =
                hystrixRestTemplateService.exchange(Md5.getMd5(FieldConstants.SYS_REQUEST_SOURCE_ID), LogServiceIdConstants.UPDATE_LOG_LEVEL_102020, getWay,
                        ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<List<Res102020>>>() {
                        });
        if (!result.isSuccess()) {
            log.error(getWay, result.getBizCode(), result.getBizMsg());
            throw new BusinessException(
                    StringUtils.isBlank(result.getBizMsg()) ? "根据应用appID获取各个服务的日志级别信息错误" : result.getBizMsg());
        }
        List<Res102020> res102020s = result.getData() == null ? new ArrayList<>() : result.getData();
        return res102020s;
    }

    /**
     * 根据应用appID获取各个服务的日志级别
     *
     * @param appId
     * @return
     */
    public String getLogNameByAppId(final String appId) {
        GatewayRequest<Req102020> getWay = new GatewayRequest<>();
        Req102020 req102020 = new Req102020();
        req102020.setAppId(appId);
        getWay.setContent(req102020);
        getWay.setMethod("102021");
        Result<Res102021> result = hystrixRestTemplateService.exchange(Md5.getMd5(FieldConstants.SYS_REQUEST_SOURCE_ID), LogServiceIdConstants.UPDATE_LOG_LEVEL_102021,
                getWay, ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res102021>>() {
                });
        if (!result.isSuccess()) {
            log.error(getWay, result.getBizCode(), result.getBizMsg());
            throw new BusinessException(
                    StringUtils.isBlank(result.getBizMsg()) ? "根据应用appID获取日志名称信息错误" : result.getBizMsg());
        }
        return result.getData() == null ? "" : result.getData().getLogName();
    }
}
